package cn.xiaolang.common.utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class LoggerUtils {

	  private static Logger log = LoggerFactory.getLogger(LoggerUtils.class);

	  public static void debug(Class<?> clazz, Object obj) {
		  if(log.isDebugEnabled()){
			  log.debug(clazz == null?"":new StringBuilder(clazz.getName()).append(":").toString()+(obj == null ? null : obj.toString()));
		  }
	  }

	  public static void debug(Class<?> clazz, String message, Object... args) {
		  if(log.isDebugEnabled()){
			  log.debug(String.format((clazz == null ? "" : new StringBuilder(clazz.getName()).append(": ").toString()) + message, args));
		  }
	  }



	  public static void debug(Class<?> clazz, String message, Object arg) {
		  if(log.isDebugEnabled()){
			  log.debug(String.format((clazz == null ? "" : new StringBuilder(clazz.getName()).append(": ").toString()) + message, arg));
		  }
	  }
	  
	  public static void debug(Class<?> clazz, Throwable e, String message, Object... args) {
		  if(log.isDebugEnabled()){
			  log.debug(String.format(new StringBuilder((clazz == null ? "" : new StringBuilder(clazz.getName()).append(": ").toString())).append(message).toString(), args) + "  " + getStackTrace(e));
		  }
	  }
	  
	  
	  public static void info(Class<?> clazz, Object obj) {
		  if(log.isInfoEnabled()){
			  log.info(clazz == null?"":new StringBuilder(clazz.getName()).append(":").toString()+(obj == null ? null : obj.toString()));
		  }
	  }

	  public static void info(Class<?> clazz, String message, Object... args) {
		  if(log.isInfoEnabled()){
			  log.info(String.format((clazz == null ? "" : new StringBuilder(String.valueOf(clazz.getName())).append(": ").toString()) + message, args));
//			  log.info(String.format((clazz == null ? "" : new StringBuilder(clazz.getName()).append(": ").toString()) + message, args));
		  }
	  }

	  public static void info(Class<?> clazz, Throwable e, String message, Object... args) {
		  if(log.isInfoEnabled()){
			  log.info(String.format(new StringBuilder((clazz == null ? "" : new StringBuilder(clazz.getName()).append(": ").toString())).append(message).toString(), args) + "  " + getStackTrace(e));
		  }
	  }
	  
	  public static void error(Class<?> clazz, Throwable e, String message, Object... args) {
		  if(log.isErrorEnabled()){
			  log.error(String.format(new StringBuilder((clazz == null ? "" : new StringBuilder(clazz.getName()).append(": ").toString())).append(message).toString(), args) + "  " + getStackTrace(e));
		  }
	  }
	  
	  public static void error(Class<?> clazz, Object obj) {
		  if(log.isErrorEnabled()){
			  log.error(clazz == null?"":new StringBuilder(clazz.getName()).append(":").toString()+(obj == null ? null : obj.toString()));
		  }
	  }

	  public static void error(Class<?> clazz, String message, Object... args) {
		  if(log.isErrorEnabled()){
			  log.error(String.format((clazz == null ? "" : new StringBuilder(clazz.getName()).append(": ").toString()) + message, args));
		  }
	  }

	  public static void warn(Class<?> clazz, Throwable e, String message, Object... args) {
		  if(log.isWarnEnabled()){
			  log.warn(String.format(new StringBuilder((clazz == null ? "" : new StringBuilder(clazz.getName()).append(": ").toString())).append(message).toString(), args) + "  " + getStackTrace(e));
		  }
	  }
	  
	  public static void warn(Class<?> clazz, Object obj) {
		  if(log.isWarnEnabled()){
			  log.warn(clazz == null?"":new StringBuilder(clazz.getName()).append(":").toString()+(obj == null ? null : obj.toString()));
		  }
	  }

	  public static void warn(Class<?> clazz, String message, Object... args) {
		  if(log.isWarnEnabled()){
			  log.warn(String.format((clazz == null ? "" : new StringBuilder(clazz.getName()).append(": ").toString()) + message, args));
		  }
	  }
	  
	  public static void trace(Class<?> clazz, Throwable e, String message, Object... args) {
		  if(log.isTraceEnabled()){
			  log.trace(String.format(new StringBuilder((clazz == null ? "" : new StringBuilder(clazz.getName()).append(": ").toString())).append(message).toString(), args) + "  " + getStackTrace(e));
		  }
	  }
	  
	  public static void trace(Class<?> clazz, Object obj) {
		  if(log.isTraceEnabled()){
			  log.trace(clazz == null?"":new StringBuilder(clazz.getName()).append(":").toString()+(obj == null ? null : obj.toString()));
		  }
	  }

	  public static void trace(Class<?> clazz, String message, Object... args) {
		  if(log.isTraceEnabled()){
			  log.trace(String.format((clazz == null ? "" : new StringBuilder(clazz.getName()).append(": ").toString()) + message, args));
		  }
	  }
	
	  
	  private static String getStackTrace(Throwable t) {
		    StringWriter sw = new StringWriter();
		    PrintWriter pw = new PrintWriter(sw, true);
		    t.printStackTrace(pw);
		    pw.flush();
		    pw.close();
		    sw.flush();
		    String trace = sw.toString();
		    try {
		      sw.close();
		    } catch (IOException e) {
		      log.info(e.getMessage());
		    }
		    return trace;
		  }
}
